<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable style="width: 200px;" placeholder="请输入模版名称" />
      <el-input v-model="listQuery.dest" clearable style="width: 200px;" placeholder="请输入目的地" />
      <el-button type="primary" icon="el-icon-search" @click="getList">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="addTemplate">新建模版</el-button>
      <el-button
        :disabled="multipleSelection.length===0"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteAll"
      >批量删除</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="模版ID" prop="id" />

      <el-table-column align="center" label="模版名称" prop="name" />

      <el-table-column align="center" label="快递公司" prop="expressCompany" />

      <el-table-column align="center" label="创建时间" prop="created" />

      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleReply(scope.row)">详情</el-button>
          <el-button
            v-permission="['POST /admin/category/update']"
            type="primary"
            size="mini"
            @click="onEdit(scope.row)"
          >编辑</el-button>
          <el-button
            v-permission="['POST /admin/category/delete']"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
    <el-dialog :visible.sync="replyFormVisible" title="详情">
      <el-form label-position="left" label-width="100px" style="width: 500px; margin-left:50px;">
        <el-form-item label="模板名称">{{ dataForm.name }}</el-form-item>
        <el-form-item label="运费方式">{{ dataForm.freightType==1?'平台包邮':'自定义' }}</el-form-item>
        <el-form-item label="默认快递公司">{{ dataForm.expressCompany }}</el-form-item>
        <el-form-item v-if="dataForm.freightType==2" label="目的地">{{ dataForm.dest }}</el-form-item>
        <el-form-item
          v-if="dataForm.freightType==2"
          label="计费类型"
        >{{ dataForm.freightType==1?"按件数":"按重量" }}</el-form-item>
        <el-form-item v-if="dataForm.freightType==2" label="首重（千克）">{{ dataForm.firstWeight }}</el-form-item>
        <el-form-item v-if="dataForm.freightType==2" label="首费（元）">{{ dataForm.firstFee }}</el-form-item>
        <el-form-item v-if="dataForm.freightType==2" label="续重（千克）">{{ dataForm.continueWeight }}</el-form-item>
        <el-form-item v-if="dataForm.freightType==2" label="续费（元">{{ dataForm.continueFee }}</el-form-item>
        <el-form-item
          v-if="dataForm.freightType==2"
          label="是否包邮"
        >{{ dataForm.zeroFeeFlag?'包邮':'不包邮' }}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="replyFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getFreightList, delFreight } from '@/api/freight/index'
import Pagination from '@/components/Pagination'
export default {
  name: 'Area',
  components: { Pagination },
  data() {
    return {
      dataForm: {},
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        courseName: null,
        index: 0,
        size: 10
      },
      replyFormVisible: false,
      multipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      if (e && e.page) {
        this.listQuery.index = e.page - 1
      }
      this.listLoading = true
      getFreightList(this.listQuery)
        .then(response => {
          this.list = response.data.data.content
          this.total = response.data.data.totalRecords
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.listLoading = false
        })
    },
    addTemplate() {
      this.$router.push({ name: 'addArea' })
    },
    resetForm() {
      this.dataForm = {
        categoryId: '',
        category: '',
        showStatus: 1,
        sortNo: ''
      }
    },
    handleReply(row) {
      this.replyFormVisible = true
      this.dataForm = Object.assign(this.dataForm, row)
    },
    handleDeleteAll() {
      const idList = []
      this.multipleSelection.forEach(item => {
        idList.push(item.id)
      })
      delFreight({ idList }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const params = {
            idList: [id]
          }
          delFreight(params).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        })
        .catch(e => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    onEdit(item) {
      this.$router.push({
        path: '/config/addArea',
        query: { data: JSON.stringify(item) }
      })
    }
  }
}
</script>
